﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;

namespace syc.Store
{
    public partial class 产品收发存统计 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.Date1.Text = DateTime.Today.ToLongDateString();
            }

        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string con1 = "select count(id) as 入库捆,sum(重量) as 入库重,sum(长度) as 入库长,sum(支数) as 入库支 from 产品库存 where 状态>0";
            string con2 = "select count(id) as 库存捆,sum(重量) as 库存重,sum(长度) as 库存长,sum(支数) as 库存支 from 产品库存 where 状态=1";
            string con3 = "select count(id) as 出库捆,sum(重量) as 出库重,sum(长度) as 出库长,sum(支数) as 出库支 from 产品库存 where 状态=2";
            int chanpin = 0;
            int fenchang = 0;
            int gonghuo = 0;

            DateTime d1, d2;
            int.TryParse(this.ChanPin.Text, out chanpin);
            int.TryParse(this.FenChang.Text, out fenchang);
            int.TryParse(this.GongHuo.Text, out gonghuo);
            if (chanpin != 0)
            {
                con1 += " and 产品类别ID=" + chanpin;
                con2 += " and 产品类别ID=" + chanpin;
                con3 += " and 产品类别ID=" + chanpin;

            }
            if (fenchang != 0)
            {
                con1 += " and 生产单位ID=" + fenchang;
                con2 += " and 生产单位ID=" + fenchang;
                con3 += " and 生产单位ID=" + fenchang;

            }
            if (gonghuo != 0)
            {
                con1 += " and 原料供货ID=" + gonghuo;
                con2 += " and 原料供货ID=" + gonghuo;
                con3 += " and 原料供货ID=" + gonghuo;

            }

            if (DateTime.TryParse(Date1.Text, out d1))
            {
                con1 += " and 入库日期>='" + d1.ToShortDateString() + "'";
                con3 += " and 出库日期>='" + d1.ToShortDateString() + "'";

            }
            else if (Date1.Text != "")
            {
                this.Label_Result.Text = "起始日期输入有误";
                return;
            }
            if (DateTime.TryParse(Date2.Text, out d2))
            {
                con1 += " and 入库日期<='" + d2.ToShortDateString() + "'";
                con3 += " and 出库日期<='" + d2.ToShortDateString() + "'";

            }
            else if (Date2.Text != "")
            {
                this.Label_Result.Text = "截止日期输入有误";
                return;
            }

            string sql = "select * from ("+con1+") t1,("+con2+") t2,("+con3+") t3";

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = sql;
            Cmd.CommandType = CommandType.Text;

            SqlDataReader Reader = Cmd.ExecuteReader();

            if (Reader.Read())
            {
                this.Label_Result.Text = "";
                if (!Reader.IsDBNull(1))
                {
                    this.Label_Result.Text = "入库捆数：" + (int)Reader[0] + "捆　　入库总重：" + (double)Reader[1] + "kg　　入库总长：" + (double)Reader[2] + "m　　入库总支数：" + (int)Reader[3] + "支<br>";
                }
                if (!Reader.IsDBNull(10))
                {
                    this.Label_Result.Text += "出库捆数：" + (int)Reader[8] + "捆　　出库总重：" + (double)Reader[9] + "kg　　出库总长：" + (double)Reader[10] + "m　　出库总支数：" + (int)Reader[11] + "支<br>";
                }
                if (!Reader.IsDBNull(5))
                {
                    this.Label_Result.Text += "库存捆数：" + (int)Reader[4] + "捆　　库存总重：" + (double)Reader[5] + "kg　　库存总长：" + (double)Reader[6] + "m　　库存总支数：" + (int)Reader[7] + "支<br>";
                }
       
            }
            Reader.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Close();
            Conn.Dispose();

        }


    }
}
